<?php


namespace App\Http\Controllers\Auth;


use App\Http\Controllers\Controller;
use App\Models\Finance\Wallet;
use App\Models\Partner\PartnerUser;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;

class LoginController extends Controller
{

    public function login(Request $request){

        $username = $this->decryptData($request->post('username'));
        $password = $this->decryptData($request->post('password'));
        $show = $this->decryptData($request->post('isShow'));
        $user = User::where('username',$username)->first();

        if(!empty($user) && $user->status == 0){
            return $this->ajaxError('該用戶異常請聯係客服');
        }
        if(empty($user) || !password_verify($password,$user->password)){
            return $this->ajaxError("用戶名或密碼錯誤");
        }else{
            if(!empty($show)){
                $partner = PartnerUser::where('user_id',$user->id)->value('partner_id');
                if(empty($partner) || $partner != 1){
                    return $this->ajaxError("無權訪問");
                }
            }
            $token = md5($this->tokenKey.$user->username);
            Cache::store('redis')->set($token,json_encode($user),3*60*60);
            return $this->ajaxSuccess($user->username);
        }

    }


    public function logout(Request $request){

        $token = $request->header('token');
        Cache::store('redis')->delete($token);
        return $this->ajaxSuccess([]);

    }



    /**
     * 找回密碼
     * @param Request $request
     */
    public function resetPassword(Request $request){

        $data = $this->decryptData($request->post());
        if($data['new_password'] != $data['new_password_confirmation']){
            return $this->ajaxError('兩次輸入密碼不一致');
        }
        $code = Cache::store('redis')->get('email'.$data['username']);
        if($code != $data['smsverify']){
            return $this->ajaxError('郵箱驗證碼錯誤');
        }
        $user = User::where('username',$data['username'])->first();
        if(!empty($user) && $user->status == 0){
            return $this->ajaxError('該用戶異常請聯係客服');
        }
        if(is_null($user)){
            return $this->ajaxError('賬戶不存在');
        }
        $user->password = password_hash($data['new_password'],PASSWORD_DEFAULT);
        $user->save();
        return $this->ajaxSuccess([]);

    }


    /**
     * 找回交易密碼
     */
    public function resetSecondaryPassword(Request $request){

        $data = $this->decryptData($request->post());
        if($data['new_password'] != $data['new_password_confirmation']){
            return $this->ajaxError('兩次輸入密碼不一致');
        }
        $code = Cache::store('redis')->get('email'.$data['username']);
        if($code != $data['smsverify']){
            return $this->ajaxError('郵箱驗證碼錯誤');
        }
        $user = User::where('username',$data['username'])->first();
        if(!empty($user) && $user->status == 0){
            return $this->ajaxError('該用戶異常請聯係客服');
        }
        if(is_null($user)){
            return $this->ajaxError('賬戶不存在');
        }

        $user->secondary_password = password_hash($data['new_password'],PASSWORD_DEFAULT);
        $user->save();
        return $this->ajaxSuccess([]);

    }

}
